package com.jxc.jxc01.yk.web;

import com.alibaba.fastjson.JSONObject;
import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.entity.RespResult;
import com.jxc.jxc01.yk.entity.Sales;
import com.jxc.jxc01.yk.service.ISalesService;
import com.jxc.jxc01.yk.service.SalesServiceImpl;
import com.jxc.jxc01.yk.util.UUIDCode;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("*.do")
public class SalesServlet extends HttpServlet {
    ISalesService salesService = new SalesServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取请求地址
        String uri = request.getRequestURI();
        System.out.println("当前进入的uri地址==》" + uri);
        uri = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));

        if (uri.equals("listSales")) {
            System.out.println("进入到=》listSales");
            listSales(request, response);
        } else if (uri.equals("deleteOneOfSales")) {
            deleteOneOfSales(request, response);
        } else if (uri.equals("updateOneOfSales")) {
            System.out.println("进入到=》updateOneOfSales");
            updateOneOfSales(request, response);
        } else if (uri.equals("addSales")) {
            System.out.println("进入到=》addSales");
            addSales(request, response);
        } else if (uri.equals("findSalesId")) {
            System.out.println("进入到=》findSalesId");
            findSalesId(request, response);
        } else if (uri.equals("findSalesName")) {
            System.out.println("进入到=》findSalesName");
            findSalesName(request, response);
        }else if (uri.equals("findSalesSalesNumber")) {
            System.out.println("进入到=》findSalesSalesNumber");
            findSalesSalesNumber(request, response);
        }
    }

    /**
     * 修改销售信息
     */
    private void addSales(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Sales> sales = new ArrayList<>();
        Sales sale = new Sales();
        //获取要增加的销售信息
        //生成一个uuid销售单号
        int salesnumber = new UUIDCode().getUuid();
        sale.setSalesnumber(salesnumber);
        sale.setSalesperson(request.getParameter("salesperson"));
        sale.setSalesgoods(request.getParameter("salesgoods"));
        sale.setVariety(request.getParameter("variety"));
        sale.setSalesname(request.getParameter("salesname"));
        sale.setUnits(request.getParameter("units"));
        String salesvolumeStr = request.getParameter("salesvolume");
        if (salesvolumeStr.equals(null) || salesvolumeStr.equals("")){
            salesvolumeStr = "0";
        }
        sale.setSalesvolume(Integer.parseInt(salesvolumeStr));
        String salespriceStr = request.getParameter("salesprice");
        if (salespriceStr.equals(null) || salespriceStr.equals("")){
            salespriceStr = "0";
        }
        sale.setSalesprice(Double.parseDouble(salespriceStr));
        sale.setTotalprice(sale.getSalesvolume() * sale.getSalesprice());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String salestime = sdf.format(date);
        sale.setSalestime(salestime);
        sale.setCondition(request.getParameter("status"));
        System.out.println(sale.toString());
        sales.add(sale);
        boolean addSales = salesService.addSales(sales);
        response.sendRedirect("xiaoshou.html");
    }

    /**
     * 修改销售信息
     */
    private void updateOneOfSales(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Sales sale = new Sales();
        //获取要修改的信息
        sale.setId(Integer.parseInt(request.getParameter("id")));
        sale.setSalesperson(request.getParameter("salesperson"));
        sale.setSalesgoods(request.getParameter("salesgoods"));
        sale.setVariety(request.getParameter("variety"));
        sale.setSalesname(request.getParameter("salesname"));
        sale.setUnits(request.getParameter("units"));
        String salesvolumeStr = request.getParameter("salesvolume");
        if (salesvolumeStr.equals(null) || salesvolumeStr.equals("")){
            salesvolumeStr = "0";
        }
        sale.setSalesvolume(Integer.parseInt(salesvolumeStr));
        String salespriceStr = request.getParameter("salesprice");
        if (salespriceStr.equals(null) || salespriceStr.equals("")){
            salespriceStr = "0";
        }
        sale.setSalesprice(Double.parseDouble(salespriceStr));
        sale.setTotalprice(sale.getSalesvolume()*sale.getSalesprice());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String salestime = sdf.format(date);
        sale.setSalestime(salestime);
        sale.setCondition(request.getParameter("status"));
        System.out.println(sale.toString());
        boolean updateSales = salesService.updateSales(sale);
        response.sendRedirect("xiaoshou.html");
    }

    /**
     * 根据id查询销售清单
     */
    private List<Sales> findSalesId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Sales> sales = new ArrayList<>();
        //获取要查找的id
        int id = Integer.parseInt(request.getParameter("id"));
        sales = salesService.findSalesId(id);
        if (sales != null && sales.size() != 0) {
            RespResult result = new RespResult();
            result.setData(sales);
            out.println(JSONObject.toJSONString(result));
        }
        //response.sendRedirect("listSales.jsp");
        return sales;
    }

    /**
     * 根据销售单号查询销售清单
     */
    private List<Sales> findSalesSalesNumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Sales> sales = new ArrayList<>();
        //获取要查找的销售单号
        String salesnumberstr = request.getParameter("salesnumber");
        if (salesnumberstr == null || salesnumberstr==""){
            salesnumberstr = "0";
        }
        int salesnumber = Integer.parseInt(salesnumberstr);
        sales = salesService.findSalesSalesNumber(salesnumber);
        if (sales != null && sales.size() != 0) {
            RespResult result = new RespResult();
            result.setData(sales);
            out.println(JSONObject.toJSONString(result));
        }
        //response.sendRedirect("listSales.jsp");
        return sales;
    }

    /**
     * 根据客户查询销售清单
     */
    private void findSalesName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Sales> sales = null;
        //获取要查找的客户的销售信息
        String salesname = request.getParameter("salesname");
        if (salesname ==null || salesname==""){
            sales = new ArrayList<>();
        }else {
            sales = salesService.findSalesName(salesname);
        }
        RespResult result = new RespResult(0, "ok", sales.size(), sales);
        HttpSession session = request.getSession();
        session.setAttribute("findnameresult", result);
        response.sendRedirect("xiaoshou.html");

    }

    /**
     * 删除某一条销售记录
     */
    private void deleteOneOfSales(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean deleteSales = salesService.deleteSales(id);
        response.sendRedirect("xiaoshou.html");
    }

    /**
     * 查询所有销售清单
     */
    private void listSales(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //查询session
        HttpSession session = request.getSession();
        RespResult findnameresult = (RespResult) session.getAttribute("findnameresult");
        PageHelper<Sales> pageHelper = new PageHelper();
        if (findnameresult == null) {
            //查询销售清单
            pageHelper = salesService.findSalesList(pageHelper);
            System.out.println("进入到listSales.do中查询的所有数据==》" + pageHelper.getData());
            if (pageHelper != null) {
                RespResult result = new RespResult(0, "ok", pageHelper.getTotal(), pageHelper.getData());
                out.print(JSONObject.toJSONString(result));
            }
        } else {
            out.print(JSONObject.toJSONString(findnameresult));
            session.removeAttribute("findnameresult");
        }


    }

}
